package com.itheima.book.manager.dao;

import com.itheima.book.manager.domain.Book;

import java.util.ArrayList;

public class BookDao2 implements BaseBookDao {
    private static ArrayList<Book> books = new ArrayList<>();
    public boolean addBook(Book book){
        books.add(book);
        return true;
    }

    public Book[] getAllBooks(){
        Book[] books1 = new Book[books.size()];
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null){
                books1[i] = books.get(i);
            }
        }
        return books1;
    }

    public void deleteByBookId(String bookId){
        int index = -1;
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null){
                if (books.get(i).getBookID().equals(bookId)){
                    index = i;
                }
            }
        }
        if (index != -1){
            books.remove(index);
        }
    }

    public void updateBookByBookId(Book book){
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null){
                if (books.get(i).getBookID().equals(book.getBookID())){
                    books.set(i,book);
                }
            }
        }
    }
}
